package com.example.a66666.yuejian.Model;

import android.app.Activity;

import com.example.a66666.yuejian.Bean.ActivityBean;
import com.example.a66666.yuejian.Bean.GameBean;
import com.example.a66666.yuejian.Bean.LoginBean;
import com.example.a66666.yuejian.Bean.MasterBean;
import com.example.a66666.yuejian.Bean.RegisterBean;
import com.example.a66666.yuejian.Interface.RetrofitService;
import com.example.a66666.yuejian.Listener.RetrofitListener;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 66666 on 2018/9/26.
 */

public class UserModel {

    String myIP="http://10.0.2.2";


     public void userLogin(String username ,String password,final RetrofitListener listener){
         Retrofit retrofit=new Retrofit.Builder()
                 .baseUrl(myIP+"/yuejian/public/index.php/api/login/")
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();
         RetrofitService service=retrofit.create(RetrofitService.class);
         Call<LoginBean> call=service.login(username,password);
         call.enqueue(new Callback<LoginBean>() {
             @Override
             public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                 listener.onSuccess(response.body());
             }

             @Override
             public void onFailure(Call<LoginBean> call, Throwable t) {
                    listener.onFail(t.toString());
             }
         });
     }


     public void userRegister(String username,String password,final RetrofitListener listener){
         Retrofit retrofit=new Retrofit.Builder()
                 .baseUrl(myIP+"/yuejian/public/index.php/api/register/")
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();
         RetrofitService service=retrofit.create(RetrofitService.class);
         Call<RegisterBean> call=service.register(username,password);
         call.enqueue(new Callback<RegisterBean>() {
             @Override
             public void onResponse(Call<RegisterBean> call, Response<RegisterBean> response) {
                 listener.onSuccess(response.body());
             }

             @Override
             public void onFailure(Call<RegisterBean> call, Throwable t) {
                  listener.onFail(t.toString());
             }
         });
     }

     public void getAllActivity(final RetrofitListener listener){
         Retrofit retrofit=new Retrofit.Builder()
                 .baseUrl(myIP+"/yuejian/public/index.php/api/activity/")
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();
         RetrofitService service=retrofit.create(RetrofitService.class);
         Call<List<ActivityBean>> call=service.getAllActivity();
         call.enqueue(new Callback<List<ActivityBean>>() {
             @Override
             public void onResponse(Call<List<ActivityBean>> call, Response<List<ActivityBean>> response) {
                 listener.onSuccess(response.body());
             }

             @Override
             public void onFailure(Call<List<ActivityBean>> call, Throwable t) {
                listener.onFail(t.toString());
             }
         });
     }

     public void getAllMaser(final RetrofitListener listener){
         Retrofit retrofit=new Retrofit.Builder()
                 .baseUrl(myIP+"/yuejian/public/index.php/api/master/")
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();
         RetrofitService service=retrofit.create(RetrofitService.class);
         Call<List<MasterBean>> call=service.getAllMaster();
         call.enqueue(new Callback<List<MasterBean>>() {
             @Override
             public void onResponse(Call<List<MasterBean>> call, Response<List<MasterBean>> response) {
                  listener.onSuccess(response.body());
             }

             @Override
             public void onFailure(Call<List<MasterBean>> call, Throwable t) {
                  listener.onFail(t.toString());
             }
         });
     }

     public void getAllGame(final RetrofitListener listener){
         Retrofit retrofit=new Retrofit.Builder()
                 .baseUrl(myIP+"/yuejian/public/index.php/api/game/")
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();
         RetrofitService service=retrofit.create(RetrofitService.class);
         Call<List<GameBean>> call=service.getAllGame();
         call.enqueue(new Callback<List<GameBean>>() {
             @Override
             public void onResponse(Call<List<GameBean>> call, Response<List<GameBean>> response) {
                 listener.onSuccess(response.body());
             }

             @Override
             public void onFailure(Call<List<GameBean>> call, Throwable t) {
                 listener.onFail(t.toString());
             }
         });
     }

    public void getActivityDetail(int activityID,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/activity/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<ActivityBean> call=service.getActivityByID(activityID);
        call.enqueue(new Callback<ActivityBean>() {
            @Override
            public void onResponse(Call<ActivityBean> call, Response<ActivityBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<ActivityBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void getGameDetail(int gameID, final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/game/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<GameBean> call=service.getGameByID(gameID);
        call.enqueue(new Callback<GameBean>() {
            @Override
            public void onResponse(Call<GameBean> call, Response<GameBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<GameBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void getMasterDetail(int masterID,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/master/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<MasterBean> call=service.getMasterByID(masterID);
        call.enqueue(new Callback<MasterBean>() {
            @Override
            public void onResponse(Call<MasterBean> call, Response<MasterBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<MasterBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void isCollect(int uid, int aid,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/collect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean> call=service.isCollectActivity(uid,aid);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void collectActivity(int uid, int aid,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/collect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean> call=service.collectActivity(uid,aid);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void cancelCollectActivity(int uid, int aid,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/collect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean> call=service.cancelCollectActivity(uid,aid);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void isCollectMaster(int uid, int mid,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/collect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean> call=service.isCollectMaster(uid,mid);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void collectMaster(int uid, int mid,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/collect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean> call=service.collectMaster(uid,mid);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void cancelCollectMaster(int uid, int mid,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/collect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean> call=service.cancelCollectMaster(uid,mid);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void isCollectGame(int uid, int gid,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/collect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean> call=service.isCollectGame(uid,gid);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void collectGame(int uid, int gid,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/collect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean> call=service.collectGame(uid,gid);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void cancelCollectGame(int uid, int gid,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(myIP+"/yuejian/public/index.php/api/collect/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean> call=service.cancelCollectGame(uid,gid);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

}
